
const DZ = {
    form: {
        //透视提示
        Prompt: function (text = "操作失败", num) {
            var html = $('<div class="dz_tishi" style="position:absolute;z-index:9999;"/>').html(text)
            if ($(".dz_tishi").length == 0) {
                html.appendTo("body").delay(1500).fadeOut(500, function () { $(this).remove(); })
                html.css({
                    left: num == 1 ? ($(window).width() - html.outerWidth()) / 2 : 0,
                    top: num == 1 ? ($(window).height() - html.outerHeight()) / 2 : 0,
                    "width": num == 1 ? "auto" : "100%",
                    "position": "fixed"
                })
            }
        }
    },
    
    pageLock: {
        show: function (index, ceng, colse, cfun) {
            if (!$("#lockpage").length) {
                var $width = ($(window).width() - $(ceng).outerWidth()) / 2
                var $height = ($(window).height() - $(ceng).outerHeight()) / 2
                var lockhtml = $("<div id='lockpage'/>").css({ position: "absolute", zIndex: (index ? index : 20), left: 0, top: 0, width: $(window).width(), height: $(document).height(), background: "#000", opacity: 0.3 }).appendTo($(".pageconversionwrap").length > 0 ? ".pageconversionwrap" : "body");
            }
            $("#lockpage").css({ opacity: 0, display: "block" }).animate({ opacity: 0.3 }, 500);
            $(ceng).css({ position: "fixed", left: $width, top: $height });
            $(ceng).show();
            $("#lockpage").click(function (e) {
                DZ.pageLock.hide(ceng)
                e.stopPropagation();
            })
            $(colse).click(function (e) {
                DZ.pageLock.hide(ceng)
                e.stopPropagation();
                return false
            });
            if (typeof cfun == "function") {
                cfun()
            }
        },
        hide: function (ceng) {
            $("#lockpage").remove()
            $(ceng).hide();
        }
    },
    storage: {
        support: function () {
            return "localStorage" in window && window['localStorage'] !== null;
        },
        get: function (key, convert) {
            if (this.support()) {
                try {

                    let val = localStorage.getItem(key);
                    if (val && convert) {
                        val = JSON.parse(val);
                    }

                    return val;

                } catch (e) {
                    return "";
                }
            } else {
                return "";
            }
        },
        set: function (key, val) {
            if (this.support()) {
                try {

                    //对象自动转换成JSON字符串
                    if (val instanceof Object) {
                        val = JSON.stringify(val)
                    }

                    localStorage.setItem(key, val);

                    return true;

                } catch (e) {
                    return false;
                }
            } else {
                return false;
            }
        },
        remove: function (key) {
            try {
                localStorage.removeItem(key);
                return true;
            } catch (e) {
                return false;
            }
        }
    },
    session:{
        get:function(_name){
            return window.sessionStorage.getItem(_name);
        },
        set:function(_name,_value){
            window.sessionStorage.setItem(_name, _value);
        },
        remove:function(_name){
            window.sessionStorage.removeItem(_name);
        },
        clear:function(){
            window.sessionStorage.clear();
        },
        getAll:function(){
            return window.sessionStorage;
        },
        isExist:function(_name){
            if(window.sessionStorage.getItem(_name)){
                return true;
            }else{
                return false;
            }
        }
    },
    regexp: {
        mobile: /^1[3-9]\d{9}$/,
        chinamobile: /^[1][3-8]\d{9}$|^([6|9])\d{7}$|^[0][9]\d{8}$|^[6]([8|6])\d{5}$/,
        mainlandMobile: /^((\+86)|(86))?[1][3-8]\d{9}$/,
        internationalPhone: /^((([\(\（]\+?\d{2,6}[\)\）])|(\+?\d{2,6}))-?)?(([\(\（]\d{6,12}[\)\）])|(\d{6,12}))$/,
        email: /^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/i,
        numlen6: /^\d{6}$/,
        password: /^[\x21-\x7E]{6,50}$/,
        name: /^[\u4e00-\u9fa50-9a-zA-Z \.]{1,20}$/,
        postcode: /^\d{6}$/,
        address: /^.{1,80}$/,
        //address: /^([\u4E00-\u9FA50-9a-zA-Z_() （）#-]){1,100}$/,
        remark: /^.{1,200}$/,
        company: /^[\u4E00-\u9FA50-9a-zA-Z_()（）#-]{1,50}$/,
        couponcode: /^[0-9a-zA-Z]{6,12}$/
    },
    date: {
        format: function (date = new Date(), fmt = "yyyy-MM-dd") {
            var o = {
                "M+": date.getMonth() + 1, //月份
                "d+": date.getDate(), //日
                "h+": date.getHours(), //小时
                "m+": date.getMinutes(), //分
                "s+": date.getSeconds(), //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(fmt)) {
                    fmt = fmt.replace(
                        RegExp.$1,
                        (RegExp.$1.length == 1)
                            ? (o[k])
                            : (("00" + o[k]).substr(("" + o[k]).length))
                    );
                }
            }
            return fmt;
        }
    },
    cookie: {
        get: function (name) {
            var arg = name + "=";
            var alen = arg.length;
            var clen = document.cookie.length;
            var i = 0;
            while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg) {
                    var endstr = document.cookie.indexOf(";", j);
                    if (endstr == -1) {
                        endstr = document.cookie.length;
                    }
                    return decodeURIComponent(document.cookie.substring(j, endstr));
                }
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0) break;
            }
            return "";
        },
        set: function (name, value, expires, path, domain, secure) {
            if (typeof expires === 'number') {
                var minutes = expires;
                expires = new Date();
                expires.setMinutes(expires.getMinutes() + minutes);
            }

            if (expires instanceof Date) {
                expires = expires.toUTCString()
            }

            document.cookie = name + "=" + encodeURIComponent(value) +
                ((expires) ? "; expires=" + expires : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
        },
        del: function (name, path, domain) {
            if (this.get(name)) {
                document.cookie = name + "=" +
                    ((path) ? "; path=" + path : "") +
                    ((domain) ? "; domain=" + domain : "") +
                    "; expires=Thu, 31 Jan 1980 16:00:00 GMT";
            }
        }
    },
    getQueryString:function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    },
    //获取客户端版本号
    vs:function(){
        var ua=window.navigator.userAgent.toLowerCase();
        var str=ua.match(/ShopZhipin\/+\d.+/i)[0];
        var vs=parseInt(str.replace(/shopzhipin\//,""))
        return vs
    },
    GetLength : function(str,type) {
        if(type){
            type=type
        }
        else
        {
            type=0
        }
        var realLength = 0, len =str.length, charCode = -1;
        for (var i = 0; i < len; i++) {
            charCode = str.charCodeAt(i);
            if(type==1){
                    if (charCode >= 0 && charCode <= 128) realLength += 0.5;
                        else 
                    { 
                      realLength += 1;
                    }
            }
            else
            {
                  if (charCode >= 0 && charCode <= 128) realLength += 1;
                    else 
                    { 
                      realLength += 1;
                    }
            }
        }
        return Math.round(realLength);
     }
}

export default DZ